/*DONE* Table [dbo].[<CompanyName>$Sales Price$437dbf0e-84ff-417a-965d-ed2bb9650972]  1/2  Script Date: 29.7.2020 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Sales Price$437dbf0e-84ff-417a-965d-ed2bb9650972]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Sales Price$437dbf0e-84ff-417a-965d-ed2bb9650972](
	[timestamp] [timestamp] NOT NULL,
	[Item No_] [nvarchar](20) NOT NULL,
	[Sales Type] [int] NOT NULL,
	[Sales Code] [nvarchar](20) NOT NULL,
	[Starting Date] [datetime] NOT NULL,
	[Currency Code] [nvarchar](10) NOT NULL,
	[Variant Code] [nvarchar](10) NOT NULL,
	[Unit of Measure Code] [nvarchar](10) NOT NULL,
	[Minimum Quantity] [decimal](38, 20) NOT NULL,
	[Unit Price] [decimal](38, 20) NOT NULL,
	[Price Includes VAT] [tinyint] NOT NULL,
	[Allow Invoice Disc_] [tinyint] NOT NULL,
	[VAT Bus_ Posting Gr_ (Price)] [nvarchar](20) NOT NULL,
	[Ending Date] [datetime] NOT NULL,
	[Allow Line Disc_] [tinyint] NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Sales Price$437dbf0e-84ff-417a-965d-ed2bb9650972$Key1] PRIMARY KEY CLUSTERED 
(
	[Item No_] ASC,
	[Sales Type] ASC,
	[Sales Code] ASC,
	[Starting Date] ASC,
	[Currency Code] ASC,
	[Variant Code] ASC,
	[Unit of Measure Code] ASC,
	[Minimum Quantity] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Sales Price$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END